package cn.cas.mango.util.paramCheck;

import cn.cas.mango.dto.param.MaterialParam;
import cn.cas.mango.util.string.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

// 检查material搜索的传入参数是否正确
@Slf4j
public class MaterialParamCheckUtil {

  public static Map<String, Object> checkQueryAnalysisParam(MaterialParam formData,int pageSize) {
    Map<String, Object> map = new HashMap<>();
    String isSuccess = MaterialParamCheckUtil.checkParam(formData);

    if (!isSuccess.equals("ok")) {
      map.put("isSuccess", isSuccess);
      return map;
    }


    // materialSources
    String materialSources = formData.getMaterialSources();
    if (Strings.isBlank(materialSources))
      formData.setMaterialSources("");



    // queryField
    String queryField = formData.getQueryField();
    if (Strings.isBlank(queryField)) {
      formData.setQueryField("all");
    } else {
      if (!queryField.equals("all") && !queryField.equals("content") && !queryField.equals(
          "title")) {
        String info = "参数[queryField]非法，取值应为[all | title | content]";
        log.info(info);
        map.put("isSuccess", info);
        return map;
      }
    }

    //搜索词  queryStr
    if (Strings.isBlank(formData.getQueryStr()))
      formData.setQueryStr("");

    //精准or模糊   searchType
    if (Strings.isBlank(formData.getSearchType())) {
      formData.setSearchType("true");
    } else {
      if (formData.getSearchType().charAt(0) == '0') {
        formData.setSearchType("true");
      } else {
        formData.setSearchType("false");
      }
      String searchType = formData.getSearchType();
      if (!(searchType.equalsIgnoreCase("true") || searchType.equalsIgnoreCase("false"))) {
        String info = "参数[searchType]非法，取值应为[true | false]";
        log.info(info);
        map.put("isSuccess", info);
        return map;
      }
    }

    if (formData.getDepartment() == null) {
      formData.setDepartment(0);
    }


    log.info("Request param:{}", formData);
    map.put("isSuccess", isSuccess);
    map.put("formData", formData);
    return map;

  }

  public static Map<String, Object> checkParam(MaterialParam formData,int pageSize) {
    Map<String, Object> map = new HashMap<>();
    String isSuccess = MaterialParamCheckUtil.checkParam(formData);

    if (!isSuccess.equals("ok")) {
      map.put("isSuccess", isSuccess);
      return map;
    }

    // pageSize
    if (formData.getPageSize() == null || formData.getPageSize() < 0)
      formData.setPageSize(pageSize);


    // materialSources
    String materialSources = formData.getMaterialSources();
    if (Strings.isBlank(materialSources))
      formData.setMaterialSources("");

    formData.setUuid(StringUtils.getUUID());


    // queryField
    String queryField = formData.getQueryField();
    if (Strings.isBlank(queryField)) {
      formData.setQueryField("all");
    } else {
      if (!queryField.equals("all") && !queryField.equals("content") && !queryField.equals(
        "title")) {
        String info = "参数[queryField]非法，取值应为[all | title | content]";
        log.info(info);
        map.put("isSuccess", info);
        return map;
      }
    }

    //搜索词  queryStr
    if (Strings.isBlank(formData.getQueryStr()))
      formData.setQueryStr("");

    //精准or模糊   searchType
    if (Strings.isBlank(formData.getSearchType())) {
      formData.setSearchType("true");
    } else {
      if (formData.getSearchType().charAt(0) == '0') {
        formData.setSearchType("true");
      } else {
        formData.setSearchType("false");
      }
      String searchType = formData.getSearchType();
      if (!(searchType.equalsIgnoreCase("true") || searchType.equalsIgnoreCase("false"))) {
        String info = "参数[searchType]非法，取值应为[true | false]";
        log.info(info);
        map.put("isSuccess", info);
        return map;
      }
    }

    // sortOrder
    if (Strings.isBlank(formData.getSortOrder())) {
      formData.setSortOrder("desc");
    } else {
      String sortOrder = formData.getSortOrder();
      if (!sortOrder.equals("desc") && !sortOrder.equals("asc")) {
        String info = "参数[sortOrder]非法，取值应为[desc | asc]";
        log.error(info);
        map.put("isSuccess", info);
        return map;
      }
    }

    // sortType
    if (Strings.isBlank(formData.getSortType())) {
      if (formData.getSearchType().equalsIgnoreCase("true")) {
        formData.setSortType("sim");
      } else {
        formData.setSortType("time");
      }
    } else {
      String sortType = formData.getSortType();
      if (!sortType.equals("time") && !sortType.equals("sim")) {
        String info = "参数[sortType]非法，取值应为[time | sim]";
        log.error(info);
        map.put("isSuccess", info);
        return map;
      }
    }

    if (formData.getDepartment() == null) {
      formData.setDepartment(0);
    }


    log.info("【{}-{}】, Request param:{}",
        formData.getDepartment(),
        formData.getUuid(),
        formData);
    map.put("isSuccess", isSuccess);
    map.put("formData", formData);
    return map;

  }

  // 这块参照Param的逻辑，之后再合并吧
  // 检查并处理的参数：currentPage、startDate、endDate、materialTypes、security、uid
  public static String checkParam(MaterialParam formData) {
    String isSuccess = "ok";
    if (formData.getCurrentPage() == null || formData.getCurrentPage() <= 0) {
      formData.setCurrentPage(1);
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
      if (Strings.isNotBlank(formData.getEndDate())) {
        formData.setEndDate(sdf.format(sdf.parse(formData.getEndDate())));
      }
      if (Strings.isNotBlank(formData.getStartDate())) {
        formData.setStartDate(sdf.format(sdf.parse(formData.getStartDate())));
      }
    } catch (Exception e) {
      String info = "参数[endDate或startDate]非法，格式应为'yyyy-MM-dd'";
      isSuccess = info;
      return isSuccess;
    }

    //materialTypes可为空，多个值使用逗号隔开
    if (Strings.isNotBlank(formData.getMaterialTypes())) {
      formData.setMaterialTypesArray(ParamCheckUtil.string2FieldValue(formData.getMaterialTypes()));
    }

    //security可为空，多个值使用逗号隔开
    if (Strings.isNotBlank(formData.getSecurity())) {
      formData.setSecurityArray(ParamCheckUtil.string2FieldValue(formData.getSecurity()));
    }

    //uid可为空，多个值使用逗号隔开
    if (Strings.isNotBlank(formData.getUid())) {
      formData.setUidArray(ParamCheckUtil.string2FieldValue(formData.getUid()));
    }

    if (Strings.isBlank(formData.getMaterialFor())) {
      formData.setMaterialFor("work");
    }

    return isSuccess;
  }


}
